<?php

namespace expressage;

use think\Request;

class Kd100 extends Expressage
{

    public function logistics($data)
    {
//        $request_data = '{"com":"' . $code . '","num":"' . $number . '","from":"","phone":"'.$phone.'","to":"","resultv2":"0","show":"0","order":"desc"}';
        $request_data = array (
            'com' => $data['express_no'],             // 快递公司编码
            'num' => $data['trade_no'],     // 快递单号
            'phone' => $data['mobile'],                // 手机号
            'from' => $data['from_address'],                 // 出发地城市
            'to' => $data['to_address'],                   // 目的地城市
            'resultv2' => '1',            // 开启行政区域解析
            'show' => '0',                // 返回格式：0：json格式（默认），1：xml，2：html，3：text
            'order' => 'desc'             // 返回结果排序:desc降序（默认）,asc 升序
        );
        $request_data = json_encode($request_data, JSON_UNESCAPED_UNICODE);
        //请求参数
        $datas = array(
            'customer'  => $this->app,
            'sign'      => strtoupper(md5($request_data . $this->key . $this->app)),
            'param'     => $request_data,
        );

        $params = "";
        foreach ($datas as $k => $v) {
            $params .= "$k=" . urlencode($v) . "&";
        }
        $params = substr($params, 0, -1);

        $url = 'http://poll.kuaidi100.com/poll/query.do?' . $params;
         // 发送post请求
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        // 第二个参数为true，表示格式化输出json
        $result = json_decode($result, true);
        if (isset($result['data'])) {
            $this->logistics_info = $result['data'];
            $this->logistics_info;
        }
        $this->error = json_encode($result, JSON_UNESCAPED_UNICODE);
        return false;
    }

}
